import operate from '@dfeidao/atom-nodejs/db/operate';
import uuid from 'uuid';

interface Message {
	role_allot_id: string;
	role_no: string;
	productid: string;
	priority: number;
	ids: string[];
}

export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	const systemid = '12';
	const { exec, update, insert } = operate(action_id, session_id, systemid);

	const group_id = uuid();
	insert('allot_conditions_group', { _id: uuid(), role_no: msg.role_no, productid: msg.productid, role_allot_id: msg.role_allot_id, priority: msg.priority, group_id, parent_group_id: "", connector: "" });
	msg.ids.forEach((v) => {
		update('allot_conditions_group', { _id: v }, { parent_group_id: group_id });
	});
	const res1 = await exec();
	if (res1.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
